﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VQWEB.administrator.sources;
using System.Web.Script.Serialization;
using VQData;
using System.Data;
using System.Web.Routing;
using VQWEB.common;
using System.Reflection;
namespace VQWEB.administrator.handlers
{
    /// <summary>
    /// Summary description for NewsHandler
    /// </summary>
    public class core_NewsHandler : IHttpHandler, IRouteHandler
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        public void ProcessRequest(HttpContext context)
        {          
            
            
            #region News
            string add_news = context.Request["add_news"];
            if (!string.IsNullOrEmpty(add_news))
            {
                BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
                var obj = bussiness.InsertOrUpdate<core_new>(add_news);
                if (obj.category_array_id != null)
                    bussiness.InsertAllInOne<core_category_new>(obj.category_array_id, "category_id", "news_id", obj.news_id);
                if (obj.tag_array_id != null)
                    bussiness.InsertAllInOne<core_tag_new>(obj.tag_array_id, "tag_id", "news_id", obj.news_id);
                context.Response.Write("Thực thi thành công !");
            }
            string view_news = context.Request["view_news"];
            if (!string.IsNullOrEmpty(view_news))
            {
                string Id = context.Request["Id"];
                if (!string.IsNullOrEmpty(Id))
                    RenderSingle(context, Convert.ToInt32(Id));
                else
                    RenderCategoryTag(context);
            }
            string list_news = context.Request["list_news"];
            if (!string.IsNullOrEmpty(list_news))
            {
                RenderList(context);
            }
           

            string delete_news = context.Request["delete_news"];
            if (!string.IsNullOrEmpty(delete_news)) {
                 BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
                 bussiness.Delete<core_new>(delete_news);
                RenderList(context);
            }

            string lockunlock_news = context.Request["lockunlock_news"];
            if (!string.IsNullOrEmpty(lockunlock_news))
            {
                BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
                bussiness.LockUnLock<core_new>(lockunlock_news, "news_active");
                RenderList(context);
            }
        }
            #endregion
        private void RenderSingle(HttpContext context,int Id)
        {
            BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
            object obj = bussiness.Get<core_new>(
               c => c.news_id.Equals(Id),
               c => new
               {
                   id = c.news_id,
                   title = c.news_title,
                   image=c.news_image,
                   description=c.news_description,
                   content = c.news_content,
                   hit=c.news_hit,
                   date = String.Format("{0: - dd/MM/yyyy }", c.news_date), 
                   ishome=c.news_ishome,
                   active = c.news_active
               });
            PropertyInfo info = obj.GetType().GetProperty("id");
            var value = info.GetValue(obj, null);

            List<object> array_category_id = bussiness.List<core_category_new>(
                null,
                null,
                n => n.news_id.Equals(value),
                null,
                n => n.category_id
                ).ToList();
            List<object> array_tag_id = bussiness.List<core_tag_product>(
              null,
              null,
             null,
              null,
              n => n.tag_id
              ).ToList();
            List<Dictionary<string, object>> listcategory = bussiness.RenderArrayCheckboxJSON(CoreData.ViewCategory("news"), array_category_id, "id", "ten", "category_array_id[]");
            List<Dictionary<string, object>> listtag = bussiness.RenderArrayCheckboxJSON(CoreData.ViewTag(), array_tag_id, "id", "keyword", "tag_array_id[]");
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("news", obj);
            dic.Add("category", listcategory);
            dic.Add("tag", listtag);
            string json = serializer.Serialize(dic);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        private void RenderList(HttpContext context) {

             DataLayer<CoreModelDataContext> layer = new DataLayer<CoreModelDataContext>();
             IList<object> list = layer.List<core_new>(
                 null,
                 null,
                 null,
                 null,
                  c => new
                  {
                      id = c.news_id,
                      title = c.news_title,
                      image=c.news_image,
                      description = c.news_description,
                      content = c.news_content,
                      hit = c.news_hit,
                      date = String.Format("{0: - dd/MM/yyyy }", c.news_date), 
                      ishome = c.news_ishome,
                      active = c.news_active
                  });
             string json = serializer.Serialize(list);
             context.Request.ContentType = "aplication/json";
             context.Response.Write(json);
            
        }
        private void RenderCategoryTag(HttpContext context)
        {
            BusinessLayer<CoreModelDataContext> business = new BusinessLayer<CoreModelDataContext>();
            var category = business.RenderArrayCheckboxJSON(CoreData.ViewCategory("product"), null, "id", "ten", "category_array_id[]");
            var tag = business.RenderArrayCheckboxJSON(CoreData.ViewTag(), null, "id", "keyword", "tag_array_id[]");
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("tag", tag);
            dic.Add("category", category);
            string json = serializer.Serialize(dic);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        public IHttpHandler GetHttpHandler(RequestContext RequestContext)
        {
            return this;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
   
}